package com.ruoyi.warning.service.impl;


import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.warning.domain.entity.Contacts;
import com.ruoyi.warning.domain.query.ContactsQuery;
import com.ruoyi.warning.domain.vo.ContactsVO;
import com.ruoyi.warning.mapper.ContactsMapper;
import com.ruoyi.warning.service.IContactsService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>
 * 告警联系人表 服务实现类
 * </p>
 *
 * @author chenhc
 * @since 2025-02-06
 */
@Service
public class ContactsServiceImpl extends ServiceImpl<ContactsMapper, Contacts> implements IContactsService {
    @Resource
    private ContactsMapper contactsMapper;
    @Override
    public TableDataInfo getContactsPage(ContactsQuery query) {
        Page<Object> page = PageHelper.startPage(query.getPageNum(),query.getPageSize());
        if(ObjectUtils.isNotEmpty(query.getCreateTimeStart())){
            query.setCreateTimeStart(query.getCreateTimeStart()+" 00:00:00");
            query.setCreateTimeEnd(query.getCreateTimeEnd()+" 23:59:59");
        }
        List<ContactsVO> list1 = contactsMapper.getList(query);
        TableDataInfo tableDataInfo = new TableDataInfo(list1, page.getTotal());
        return tableDataInfo;
    }

    @Override
    public List<ContactsVO> getContactsList(ContactsQuery query) {
        return contactsMapper.getList(query);
    }

    @Override
    public void export(HttpServletResponse response) {
        List<Contacts> list = this.list();
        ExcelUtil<Contacts> util = new ExcelUtil<>(Contacts.class);
        util.exportExcel(response, list, "联系人表单");
    }

    @Override
    public String saveContacts(Contacts contacts) {
        if(ObjectUtils.isNotEmpty(this.getOne(new LambdaUpdateWrapper<Contacts>()
                .eq(Contacts::getPhoneNumber,contacts.getPhoneNumber())
                .eq(Contacts::getContactsName,contacts.getContactsName())))){
            throw new ServiceException("联系人已存在");
        }
        this.save(contacts);
        return "ok";
    }

    @Override
    public String deleteContacts(List<String> ids) {
        this.removeByIds(ids);
        return "ok";
    }

    @Override
    public String updateContacts(Contacts contacts) {
        if(ObjectUtils.isNotEmpty(this.getOne(new LambdaUpdateWrapper<Contacts>()
                .eq(Contacts::getPhoneNumber,contacts.getPhoneNumber())
                .eq(Contacts::getContactsName,contacts.getContactsName())
                .ne(Contacts::getId,contacts.getId())))){
            throw new ServiceException("联系人已存在");
        }
        this.updateById(contacts);
        return null;
    }

    @Override
    public Contacts getDetails(Long id) {
        return this.getById(id);
    }

    @Override
    public String updateContactStatus(Contacts contacts) {
        LambdaUpdateWrapper<Contacts> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(Contacts::getId,contacts.getId())
                .set(Contacts::getStatus,contacts.getStatus());
        this.update(wrapper);
        return null;
    }




}
